import datetime as datetime
import sqlite3

import matplotlib.dates as mpd
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

import iplot.iplothelper as hp
from mpl_finance import (candlestick2_ohlc, candlestick_ohlc,
                         plot_day_summary_oclh)

fig, ax = plt.subplots()




start_date = '2016-01-08'
end_date = '2016-01-11'
start_timestamp = hp.dateStr2timestamp(start_date,'%Y-%m-%d')
end_timestamp = hp.dateStr2timestamp(end_date,'%Y-%m-%d')


quotes = hp.getdata_to_USALine('eurusd_15',start_timestamp,end_timestamp)





quotes = hp.getdata_to_USALine('eurusd_15',start_timestamp,end_timestamp)
# print(datestr_list)
plot_day_summary_oclh(ax, quotes, ticksize=3, colorup='r', colordown='k')


rectangle = patches.Rectangle(
        (quotes[0][0], quotes[0][1]),  # (x,y)
        quotes[10][0]-quotes[0][0],  # width 往后几根
        quotes[10][1] - quotes[0][1],  # 高度
        color="#9a9797",
        alpha=0.5  # 透明度
    )
    # print(width*mark,size)
    # print(high)
    # 在最低点下面标注文字

    # 绘制矩形
ax.add_patch(rectangle)

plt.show()
# ax.xaxis.set_major_formatter(formatter)
from matplotlib.ticker import Formatter
# class MyFormatter(Formatter):
#     def __init__(self, dates, fmt='%Y-%m-%d'):
#         # print(dates)
#         self.dates = dates
#         self.fmt = fmt

#     def __call__(self, x, pos=0):
#         # l = []
#         # print(x)
#         # for p  in quotes:
#         #     d2n = p[0]
#         #     s = 
#         # l.append(s)
#         return mpd.num2date(x).strftime('%c')

# formatter = MyFormatter(quotes)
# ax.xaxis.set_major_formatter(formatter)

plot_day_summary_oclh(ax, quotes, ticksize=3, colorup='r', colordown='k')
s = '2016-01-08'
s2 = '2016-01-09'
d1 = hp.str_to_datetime(s,'%Y-%m-%d')
d2 = hp.str_to_datetime(s2,'%Y-%m-%d')
d2n1 = mpd.date2num(d1)
d2n2 = mpd.date2num(d2)

ax.xaxis_date()
ax.autoscale_view()
hp.draw_rectangle2(ax,quotes,d2n1,d2n2)

plt.show()
